package com.zd.bms.controller;


import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zd.bms.bean.vo.TicketOrderDetailVo;
import com.zd.bms.service.ITicketInfoService;
import com.zd.bms.service.ITicketOrderDetailService;
import com.zd.bms.service.ITicketOrderService;
import com.zd.common.bean.model.BmsUser;
import com.zd.common.bean.model.ShortQrcode;
import com.zd.common.bean.model.TicketInfo;
import com.zd.common.bean.model.TicketOrderDetail;
import com.zd.common.common.utils.DateTimeUtil;
import com.zd.common.common.utils.LogUtils;
import com.zd.common.common.utils.QrCodeCreateUtil;
import com.zd.common.common.utils.StringUtils;
import com.zd.common.dao.ShortQrcodeDao;


@Controller
@RequestMapping("/takeOrRetreat")
public class TakeOrRetreatController extends BaseController{
	
	@Autowired
	private ITicketOrderService ticketOrderService;
	
	@Autowired
	private ITicketInfoService ticketInfoService;
	
	@Autowired
	private ITicketOrderDetailService ticketOrderDetailService ;
	
	@Autowired
	private ShortQrcodeDao shortQrcodeDao;
	
	@RequestMapping("/toPickUpTticket")
	public ModelAndView toPickUpTticket(Integer modeType){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/sellTicket/takeOrRetreat/pickUpTticket");
		mv.addObject("modeType", modeType);
		return mv;
	}
	
	@RequestMapping("/toIdNumPick")
	public ModelAndView toIdNumPick(Integer modeType){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/sellTicket/takeOrRetreat/idNumPick");
		mv.addObject("modeType", modeType);
		return mv;
	}
	
	@RequestMapping("/toCheckCodePick")
	public ModelAndView toCheckCodePick(Integer modeType) throws Exception{
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/sellTicket/takeOrRetreat/checkCodePick");
		mv.addObject("modeType", modeType);
		mv.addObject("user", getSession("user"));
		return mv;
	}
	
	@RequestMapping("checkCodePick")
	public ModelAndView checkCodePick(String checkCode,Integer modeType)throws Exception{
		ModelAndView mv = new ModelAndView();
		Map<String, Object> resultMap = ticketOrderService.getTicketOrderDetailList(null, checkCode, modeType,getSessionUser());
		mv.addObject("resultMap",resultMap );
		mv.addObject("modeType",modeType );
		LogUtils.info(resultMap);
		mv.addObject("user", getSession("user"));
		mv.setViewName("/sellTicket/takeOrRetreat/userTicket");
		return mv;
	}
	
	@RequestMapping("idNumPick")
	public ModelAndView idNumPick(String idNumber,Integer modeType)throws Exception{
		ModelAndView mv = new ModelAndView();
		Map<String, Object> resultMap = ticketOrderService.getTicketOrderDetailList(idNumber, null, modeType,getSessionUser());
		mv.addObject("resultMap",resultMap );
		mv.addObject("modeType",modeType );
		mv.addObject("user", getSession("user"));
		LogUtils.info(resultMap);
		mv.setViewName("/sellTicket/takeOrRetreat/userTicket");
		return mv;
	}
	
	@RequestMapping("/refundTicket")
	@ResponseBody
	public Map<String, Object> refundTicket(String orderDetailIdList) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("result", true);
		BmsUser bmsUser = getSessionUser();
		resultMap.putAll(ticketOrderDetailService.refundTicket(orderDetailIdList, bmsUser));
		return resultMap;
	}
	
	@RequestMapping("/getTicket")
	@ResponseBody
	public Map<String, Object> getTicket(Integer orderDetailId) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("result", true);
		TicketOrderDetail ticketOrderDetail = ticketOrderDetailService.get(orderDetailId);
		TicketOrderDetailVo ticketOrderDetailVo= new TicketOrderDetailVo();
		BeanUtils.copyProperties(ticketOrderDetail, ticketOrderDetailVo);
		ShortQrcode shortQrcode =new ShortQrcode();
		String uuid = StringUtils.getRandomWithTimestamp16();
		shortQrcode.setUuid(uuid);
		shortQrcode.setOrderDetailId(ticketOrderDetail.getId());
		shortQrcode.setScenicSpotId(ticketOrderDetail.getScenicSpotId());
		shortQrcode.setOrderId(ticketOrderDetail.getOrderId());
		shortQrcode.setCheckCode(ticketOrderDetail.getCheckCode());
		shortQrcodeDao.insert(shortQrcode);
		
		ticketOrderDetailVo.setQrCodeStr(shortQrcode.getUuid());
		ticketOrderDetailVo.setQrCode(QrCodeCreateUtil.generalQRCode(ticketOrderDetailVo.getQrCodeStr()));
		
		ticketOrderDetailVo.setEntryTimeStr(DateTimeUtil.date2String(ticketOrderDetail.getEntryTime()));
		ticketOrderDetailVo.setValidTimeStr(DateTimeUtil.date2String(ticketOrderDetail.getValidTime()));
		
		TicketInfo ticketInfo = ticketInfoService.get(ticketOrderDetail.getTicketId());
		ticketOrderDetailVo.setPrices(ticketInfo.getPrice(), ticketInfo.getChildRate());
		ticketOrderDetailVo.setEffectiveNum(ticketInfo.getVerifyType());
		resultMap.put("result", true);
		resultMap.put("ticketOrderDetailVo", ticketOrderDetailVo);
		return resultMap;
	}
	
	@RequestMapping("/printTicket")
	@ResponseBody
	public Map<String, Object> printTicket(String checkCode , Integer orderDetailId) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("result", true);
		BmsUser bmsUser = getSessionUser();
		resultMap.putAll(ticketOrderDetailService.printTicket(checkCode, orderDetailId, bmsUser));
		return resultMap;
	}
	

	@RequestMapping("/inspectTicketJson")
	@ResponseBody
	public Map<String, Object> inspectTicket(Integer orderId, Integer orderDetailId,String checkCode,String PARK_ID,HttpServletRequest request) throws Exception {//验票接口
		String appId = PARK_ID;
		LogUtils.info(orderDetailId);
		LogUtils.info(checkCode);
		Map<String, Object> resultMap = new HashMap<>();
		resultMap = ticketOrderDetailService.inspectTicket(orderId, orderDetailId, checkCode, appId , null);
		return resultMap;
	}
	
	@RequestMapping("/inspectTicket")
	@ResponseBody
	public Map<String, Object> inspectTicketJson(String license,String PARK_ID,HttpServletRequest request){//验票接口
		LogUtils.info("license = " + license);
		LogUtils.info("PARK_ID = " + PARK_ID);
		String appId = PARK_ID;
		Map<String, Object> resultMap = new HashMap<>();
		try {
			if(license.length() == 20){
				resultMap = ticketOrderDetailService.inspectTicket(null, null, null , appId, license);
			}else if(license.length() == 16){
				resultMap = ticketOrderDetailService.getCheckCode(license, null);
				ShortQrcode shortQrcode = (ShortQrcode) resultMap.get("shortQrcode");
				resultMap = ticketOrderDetailService.inspectTicket(shortQrcode.getOrderId(), shortQrcode.getOrderDetailId(), shortQrcode.getCheckCode() , appId ,null);
			}else {
				resultMap.put("result", "111111");
				resultMap.put("message", "验票出错");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			resultMap.put("result", "111111");
			resultMap.put("message", "验票出错");
			e.printStackTrace();
			return resultMap;
		}
		System.out.println(resultMap);
		System.out.println(resultMap.get("result"));
		return resultMap;
	}
	
	/**
	 * 根据二维码的信息找去checkCode
	 * @param uuid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getCheckCode")
	@ResponseBody
	public Map<String, Object> getCheckCode(String uuid)throws Exception{
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("result", false);
		resultMap.putAll(ticketOrderDetailService.getCheckCode(uuid, getSessionUser()));
		return resultMap;
	}

}
